package org.budo.support.spring.scheduling.util;

import java.util.Date;

import org.springframework.scheduling.support.CronSequenceGenerator;
import org.springframework.util.StringUtils;

import lombok.extern.slf4j.Slf4j;

/**
 * @author limingwei
 */
@Slf4j
public class CronUtil {
    public static Date nextFromNow(String cron) {
        String[] fields = StringUtils.tokenizeToStringArray(cron, " ");
        if (fields == null || fields.length != 6) {
            log.error("#18 cron = " + cron + ", not ok");
            return null;
        }

        CronSequenceGenerator cronSequenceGenerator = new CronSequenceGenerator(cron);
        Date now = new Date();
        return cronSequenceGenerator.next(now);
    }
}